﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.IdentityModel.Tokens;
using S.BreedingHistory.Api.Application.Commmand;
using S.BreedingHistory.Api.Dto;
using S.BreedingHistory.Domain.Model;
using S.BreedingHistory.Infrastructure.Interface;
using S.Common.helper;

namespace S.BreedingHistory.Api.Application.CommmandHandler
{
    public class BreedHistoryLogicDelCommandHandler : IRequestHandler<BreedHistoryLogiDelCommand, bool>
    {
        private readonly IBreedingHistoryRepository breedingHistoryRepository;
        private readonly IMapper mapper;
        private readonly IConfiguration configuration;
        private readonly IHttpContextAccessor httpContextAccessor;

        public BreedHistoryLogicDelCommandHandler(IBreedingHistoryRepository breedingHistoryRepository,IMapper mapper,IConfiguration configuration, IHttpContextAccessor httpContextAccessor)
        {
         
            this.breedingHistoryRepository = breedingHistoryRepository;
            this.mapper = mapper;
            this.configuration = configuration;
            this.httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 种母历史记录逻辑删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<bool> Handle(BreedHistoryLogiDelCommand request, CancellationToken cancellationToken)
        {

            string authHeader = httpContextAccessor.HttpContext.Request.Headers["Authorization"].FirstOrDefault();
            if (!string.IsNullOrEmpty(authHeader) && authHeader.StartsWith("Bearer"))
            {
                authHeader = authHeader.Substring("Bearer ".Length).Trim();
            }
            BreedingHistoryModel breedingHistoryModel=mapper.Map<BreedingHistoryModel>(request);
           
            //var localConsul = configuration["LocalConsul"];
            //var url = localConsul + "/ReadBreed/api/ReadMaternalMating/ShowRamEar?ear=" + request.EarTag;
            //var Breeding= HttpClientHelper.GetResponse<BreedingDto>(url);
            breedingHistoryModel.IsDel = false;
           return await breedingHistoryRepository.UpdateAsync(breedingHistoryModel);
            //http://10.31.56.5:8087/api/ReadMaternalMating/ShowRamEar?ear=SFK2
            ////
            //bool isOk=false;
            //if (Breeding!=null)//如果已配种
            //{
            //    isOk = false;
            //    return isOk;
            //}
            //else if(Breeding == null&&request.IsEditor==true)//是导入且最新的数据
            //{
            //    isOk = true;
                
            //}
            //return isOk;
        }
    }
}
